<template>
    <a-modal
      :bodyStyle="bodyStyle"
      :title="title"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :destroyOnClose="true"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-form layout='vertical' :form="form">
        <a-form-item  class="a-form-item" v-bind="formItemLayout" label="试用期">
          <a-range-picker
            v-decorator="[
              'trialDate',
              {
                rules: [{ required: true, message: '请输入4到16位（字母，数字)的帐号!' }],
              }
            ]"/>
        </a-form-item>
        <a-form-item  class="a-form-item" v-bind="formItemLayout" label="有效期">
          <a-range-picker
            v-decorator="[
              'validityTerm',
              {
                rules: [{ required: false, message: '请输入4到16位（字母，数字)的帐号!' }],
              }
            ]"/>
        </a-form-item>
        <a-form-item>
          <a-input type="hidden" v-decorator="['id', {}]"/>
        </a-form-item>
      </a-form>
    </a-modal>
</template>
<script>
import Vue from 'vue'
import moment from 'moment'
// import api from '@/core/Api'
import serv from './AppsService'
import { Form, DatePicker, Input, Modal } from 'ant-design-vue'
Vue.component(Form.name, Form)
Vue.component(Form.Item.name, Form.Item)
Vue.component(DatePicker.RangePicker.name, DatePicker.RangePicker)
Vue.component(Input.name, Input)
Vue.component(Modal.name, Modal)
export default {
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {

  },
  data () {
    return {
      confirmLoading: false,
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        }
      },
      bodyStyle: { height: 400 },
      loading: false
    }
  },
  props: {
    title: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      default: false
    },
    record: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  watch: {
    visible: function (nvalue) {
      // !nvalue || this.getTree()
      if (nvalue && Object.keys(this.record).length > 0) {
        const {trialStartDate, trialEndDate, startDate, endDate, id} = this.record
        const filedsValue = {id}
        if (trialEndDate) {
          filedsValue.trialDate = [moment(trialStartDate, 'YYYY-MM-DD'), moment(trialEndDate, 'YYYY-MM-DD')]
        }
        if (endDate) {
          filedsValue.validityTerm = [moment(startDate, 'YYYY-MM-DD'), moment(endDate, 'YYYY-MM-DD')]
        }
        setTimeout(() => {
          this.form.setFieldsValue(filedsValue)
        }, 10)
      } else {
        this.confirmLoading = false
      }
    }
  },
  methods: {
    handleOk () {
      const form = this.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }
        console.log(values)
        let formData = {}
        if (values.id) {
          formData.id = values.id
        }
        if (values.trialDate) {
          formData.trialStartDate = values.trialDate[0].format('YYYY-MM-DD')
          formData.trialEndDate = values.trialDate[1].format('YYYY-MM-DD')
        }
        if (values.validityTerm) {
          formData.startDate = values.validityTerm[0].format('YYYY-MM-DD')
          formData.endDate = values.validityTerm[1].format('YYYY-MM-DD')
        }
        this.confirmLoading = true
        serv.save(formData).then(res => {
          this.confirmLoading = false
          if (res.data.code === 0) {
            form.resetFields()
            this.$emit('ok', formData.id)
          } else {
            this.$message.error(res.data.msg)
          }
        })
      })
    },
    handleCancel (e) {
      this.$emit('cancel')
    }
  }
}
</script>

<style scoped>
img {
  max-width: 100%;
}

.a-form-item {
  margin-bottom: 0px;
}
</style>
